package com.bw.day2task.mvp.view;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Environment;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.blankj.utilcode.util.ToastUtils;

import com.bw.day2task.R;
import com.bw.day2task.bean.BeanVideo;
import com.bw.day2task.mvp.content.LogContent;
import com.bw.day2task.mvp.model.LogModel;
import com.bw.day2task.mvp.presenter.LogPresenter;
import com.bw.library.mvp.view.BaseActivity;

import java.util.regex.Pattern;

public class LogAct extends BaseActivity<LogPresenter> implements LogContent.View {
    private ImageView img;
    private EditText logUsername;
    private EditText logUserpass1;
    private EditText logUserpass2;
    private EditText log_userphone;

    private Button logOk;

    @Override
    public int bandLayout() {
        return R.layout.act_log;
    }

    @Override
    public void initData() {
        View decorView = getWindow().getDecorView();
        decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        getWindow().setStatusBarColor(Color.TRANSPARENT);
        img.setImageResource(R.drawable.mn3);
        String username = logUsername.getText().toString().trim();
        String pass1 = logUserpass1.getText().toString().trim();
        String pass2 = logUserpass2.getText().toString().trim();
        mPresenter=new LogPresenter(this,new LogModel());
        logOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = logUsername.getText().toString().trim();
                String pass1 = logUserpass1.getText().toString().trim();
                String pass2 = logUserpass2.getText().toString().trim();
                String phone = log_userphone.getText().toString().trim();
                Judjename(username);
                if (TextUtils.isEmpty(username)||TextUtils.isEmpty(pass1)||TextUtils.isEmpty(pass2)){
                    ToastUtils.showShort("账户或密码不能为空！！");
                }else {
                   if (Judjename(username)){
                       if (Judjenpass(pass1)){
                           if (pass1.equals(pass2)){
                               if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
                                   SharedPreferences sp = getSharedPreferences("user", 0);
                                   SharedPreferences.Editor edit = sp.edit();
                                   edit.putString("name",username);
                                   edit.putString("pass",pass1);
                                   edit.putString("phone",phone+"");
                                   edit.commit();
                                   mPresenter.initLog();

                               }
                           }else {
                               ToastUtils.showShort("两次密码有误！");
                           }
                       }else {
                           ToastUtils.showShort("密码格式错误！");
                       }
                   }else {
                       ToastUtils.showShort("用户名格式错误！");
                   }
                }
            }



        });
    }
    private boolean Judjenpass(String pass1) {
        String name = "^[0-9a-zA-Z]{3}$";
        boolean matches = Pattern.matches(name, pass1);
        return matches;
    }

    private boolean Judjename(String username) {
        String name = "^[0-9a-zA-Z]{0,7}$";
        boolean matches = Pattern.matches(name, username);
        return matches;
    }

    @Override
    public void initView() {

        img = (ImageView) findViewById(R.id.img);
        logUsername = (EditText) findViewById(R.id.log_username);
        logUserpass1 = (EditText) findViewById(R.id.log_userpass1);
        logUserpass2 = (EditText) findViewById(R.id.log_userpass2);
        log_userphone = (EditText) findViewById(R.id.log_userphone);
        logOk = (Button) findViewById(R.id.reg_ok);
    }

    @Override
    public void ShowOk() {
        ToastUtils.showShort("注册成功！");
        Intent intent = new Intent(LogAct.this, RegAct.class);
        startActivity(intent);
    }

    @Override
    public String getname() {
        return logUsername.getText().toString().trim();
    }

    @Override
    public String getpass() {
        return logUserpass1.getText().toString().trim();
    }

    @Override
    public void getvideo(BeanVideo beanVideo) {

    }
}
